package com.saile.controller.web.pioneer.news;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saile.controller.base.BaseController;
import com.saile.def.Constant;
import com.saile.domain.News;
import com.saile.domain.ViewsQuantity;
import com.saile.result.ResultObj;
import com.saile.service.AdvertService;
import com.saile.service.MediaService;
import com.saile.service.MessageBoardService;
import com.saile.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 * 先锋号controller
 */
@RequestMapping("/pioneer/news")
@Controller
public class NewsController extends BaseController {
    @Autowired
    AdvertService advertService;
    @Autowired
    NewsService newsService;
    @Autowired
    MediaService mediaService;
    @Autowired
    MessageBoardService messageBoardService;

    /**
     * 先锋号首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/news/index");
        return mov;
    }

    @RequestMapping(value = "/newsList")
    public ModelAndView newsList(Byte newsType) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/news/newsList");
        mov.addObject("newsType",newsType);
        return mov;
    }

    @RequestMapping(value = "/news")
    public ModelAndView news(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/news/news");
        Map<String,String> map=new HashMap<>();
        map.put("tid",String.valueOf(tid));
        News news=newsService.getNews(map);
        mov.addObject("news",news);
        addViewsQuantity(tid, ViewsQuantity.RESOURCE_TYPE_NEWS);
        return mov;
    }

    /**
     * 获取新闻列表
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getNewsList")
    @ResponseBody
    public String getNewsList(Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            Map<String,String> map=super.showRequestParameters();
            map.put("page",String.valueOf(page));
            map.put("pageSize",String.valueOf(pageSize));
            PageHelper.startPage(page, pageSize);
            List<News> newsList = newsService.getNewsList(map);
            PageInfo<News> pageinfo = new PageInfo<News>(newsList);
            resultObj.setData(pageinfo);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 热门推荐
     *
     * @return
     */
    @RequestMapping(value = "/newsRecommend")
    public ModelAndView newsRecommend() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/news/newsRecommend");

        List<News> newsList=newsService.getNewsRecommendList(null);
        mov.addObject("newsList",newsList);
        return mov;
    }

    @RequestMapping(value = "/shcool")
    public ModelAndView shcool() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/news/shcool");

        List<News> newsList=newsService.getNewsRecommendList(null);
        mov.addObject("newsList",newsList);
        return mov;
    }


    @RequestMapping(value = "/readingList")
    public ModelAndView readingList() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/news/readingList");

        List<News> newsList=newsService.getNewsRecommendList(null);
        mov.addObject("newsList",newsList);
        return mov;
    }

}
